﻿using Architecture.Repository.Impl.Traceability;
using Architecture.Repository.Interface.Traceability;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traceability.Domain.SupplierDomain;
using Traceability.Domain.TraceabilityDto;

namespace Traceability.Domain.Service.RawMaterialServices
{
    /// <summary>
    /// 原料服务
    /// </summary>
    public class RawMaterialService : IRawMaterialService
    {
        /// <summary>
        /// 包材仓储
        /// </summary>
        protected readonly IRawMaterialRepository rawMaterialRepository;

        /// <summary>
        /// 图片仓储
        /// </summary>
        protected readonly IPictureRepository pictureRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="rawMaterialRepository">包材仓储</param>
        /// <param name="pictureRepository">图片仓储</param>
        public RawMaterialService(IRawMaterialRepository rawMaterialRepository, IPictureRepository pictureRepository)
        {
            this.rawMaterialRepository = rawMaterialRepository;
            this.pictureRepository = pictureRepository;
        }


        public async Task<int> AddRawMaterial(RawMaterial traceabilities)
        {
            return await rawMaterialRepository.AddAsync(traceabilities);
        }

        public async Task<int> DeleteRawMaterial(int rawMaterialId, string userMame)
        {
            var res = await rawMaterialRepository.GetModelAsync(rawMaterialId);
            res.DeletedBy = userMame;
            res.DeletedDate = DateTime.Now;
            res.IsDeleted = true;
            return await rawMaterialRepository.UpdateAsync(res);
        }

        public async Task<List<RawMaterialDto>> GetAllRawMaterialList(int shoppingId)
        {
            var list = (from a in await rawMaterialRepository.GetAllAsync()
                        join b in await pictureRepository.GetAllAsync()
                        on a.RawMaterialId equals b.RawMaterialId
                        where a.ShoppingId == shoppingId && !a.IsDeleted && !b.IsDeleted
                        select new RawMaterialDto
                        {
                            RawMaterialId = a.RawMaterialId,
                            RawMaterialProductionBatch = a.RawMaterialProductionBatch,
                            PictureId = b.PictureId,
                            PictureName = b.PictureName,
                        }).ToList();
            return list;
        }

        public async Task<RawMaterial> GetRawMaterialModel(int rawMaterialId)
        {
            return await rawMaterialRepository.GetModelAsync(rawMaterialId);
        }

        public async Task<int> UpdateRawMaterial(RawMaterial rawMaterial)
        {
            var res = await rawMaterialRepository.GetModelAsync(rawMaterial.RawMaterialId);
            res.RawMaterialId = rawMaterial.RawMaterialId;
            res.UpdatedBy = rawMaterial.UpdatedBy;
            res.RawMaterialProductionBatch = rawMaterial.RawMaterialProductionBatch;
            res.UpdatedDate = DateTime.Now;
            return await rawMaterialRepository.UpdateAsync(res);
        }
    }
}
